package org.joinme.backend.controller;

import org.joinme.backend.model.ApplicationRecord;
import org.joinme.backend.service.ApplicationService;
import org.joinme.backend.service.ParticipateService;
import org.joinme.backend.service.WechatUserService;
import org.joinme.backend.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = {"/{appName}/wechat/participate"}, produces = {"application/json;charset=UTF-8"})
public class ParticipateController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WechatUserService userService;

    @Autowired
    private ParticipateService participateService;

    //取消参与
    @ResponseBody
    @RequestMapping("/record/cancel")
    public String cancelParticipate(HttpServletRequest request, @RequestParam int userId,@RequestParam int carPoolingId){
        try {
            if(userId == - 1){
                //获取登陆的用户信息
                userId = userService.getLoginUserId(request);
            }
            //取消参与
            int result = participateService.cancelParticipate(userId, carPoolingId);
            if(result > 0) {
                return ApiResult.SUCCESS;
            } else
                throw new PrjException(ErrorCodeMsg.PARTICIPATE_RECORD_CANCEL_ERROR);
        } catch (PrjException e){
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error("Error message : " + e.getErrMsg());
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

}
